﻿using RabbitMQ.Client;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RMQLab.TaskDistibutor
{
    class Program
    {
        static void Main(string[] args)
        {
           // Send(args);
            //Publish(args);
            PublishWithRoute(args);
        }
        private static string GetMessage(string[] args)
        {
            return ((args.Length > 0) ? string.Join(" ", args) : "Hello World!");
        }
        static void Send(string[] args)
        {
            var factory = new ConnectionFactory() { HostName = "localhost" };
            using (var connection = factory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    channel.QueueDeclare("hello", false, false, false, null);
                    string message = GetMessage(args);
                    
                    //for (int i = 0; i < 100; i++)
                    //{
                        var body = Encoding.UTF8.GetBytes(message);
                        var properties = channel.CreateBasicProperties();
                        properties.DeliveryMode = 2;
                        channel.BasicPublish(string.Empty, "hello", properties, body);
                        Console.WriteLine(" [x] Sent {0}", message );
                    //}

                }
            }
        }

        static void Publish(string[] args)
        {

            var factory = new ConnectionFactory() { HostName = "localhost" };
            using (var connection = factory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    string message = GetMessage(args);
                    channel.ExchangeDeclare("logs", "fanout");
                    for (int i = 0; i < int.MaxValue; i++)
                    {
                    var body = Encoding.UTF8.GetBytes(message+i);
                    channel.BasicPublish("logs", string.Empty, null, body);
                    Console.WriteLine(" [x] Sent {0}", message+i);
                    }

                }
            }
        }
        static void PublishWithRoute(string[] args)
        {
            string message=string.Empty;
            using (var reader = File.OpenText(@"D:\Program Files\DebugDiag\Logs\DbgSVC_Date__11_13_2013__Time_05_53_09PM__517__Log.txt"))
            {
                message = reader.ReadToEnd();
            }
            if (args.Length==0)
            {
                args = new string[] { "info", "info log message" };
            }
            var factory = new ConnectionFactory() { HostName = "localhost" };
            using (var connection = factory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    string severity = args[0];
                    message = args[1]+ ":" +message;
                    string exchangeName= "logs-routed";
                    channel.ExchangeDelete(exchangeName);
                    channel.ExchangeDeclare(exchangeName, "direct");
                    var properties = channel.CreateBasicProperties();
                    properties.Expiration = "10";
                    int j = 0;
                    while (true)
                    {
                        for (int i = 0; i < 10; i++)
                        {
                            int id = j * 10 + i;
                            var body = Encoding.UTF8.GetBytes(id + message);
                            channel.BasicPublish("logs-routed", severity, null, body);
                            Console.WriteLine(" [x] Sent {0} {1}", severity, args[1] + id);
                        }
                        Console.Write("press any key to send more");
                        Console.ReadLine();
                        j++;
                    }

                }
            }
        }
    }
}
